﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel.Design;
using Typhone.Public.Security;
using micsun.Logic.Security;

namespace Hg401.Public.WebControls
{
    #region Designer
    public class PicMenuDesigner : System.Web.UI.Design.ControlDesigner
    {
        public PicMenuDesigner()
            : base()
        {
        }

        // Returns the html to use to represent the control at design time.
        public override string GetDesignTimeHtml()
        {
            StringBuilder str = new StringBuilder();
            str.Append("<tr>")
                .Append("<td align='center'>").Append("PicMenu").Append("</td>")
                .Append("</tr>");
            return str.ToString();
        }
    }
    #endregion
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:PicMenu runat=server></{0}:PicMenu>")]
    [ParseChildren(true)]
    [ValidationPropertyAttribute("Value")]
    [DesignerAttribute(typeof(PicMenuDesigner), typeof(IDesigner))]
    public class PicMenu : WebControl, INamingContainer
    {
        

        #region proterty...
        private FunctionNode _functionNode;
        public FunctionNode DataSource
        {
            get
            {
                return _functionNode;
            }
            set
            {
                _functionNode = value;
            }
        }
        private string _appPath = "~";
        public string AppPath
        {
            get
            {
                return _appPath;
            }
            set
            {
                _appPath = value;
            }
        }
        private Table _table;

        Permission _permission;
        public Permission Permission
        {
            get
            {
              
                return _permission;
            }

            set { _permission = value; }
        }
        
        #endregion

        public PicMenu(): base(HtmlTextWriterTag.Div) {
		 }

        protected override void OnPreRender(EventArgs e)
        {
            this.DataBind();
        }
        protected override void CreateChildControls() {
			if( _table == null )
				_table = new Table();

			_table.CellPadding = 0;
			_table.CellSpacing = 0;
            //_table.Attributes["background"] = string.Format("{0}App_Themes/Default/images/1_pic_3.jpg", this._appPath);
            //_table.Width = Unit.Percentage( 100 );
            //_table.Height=Unit.Percentage( 100 );
			

			this.Controls.Add( _table );
		}
	
        public override void DataBind()
        {
            EnsureChildControls();

            if (this._functionNode == null)
            {
                //this._functionNode = FunctionSystem.get.GetRootNode();
                return;
            }
            
            CreateTable(this._functionNode);
        }

        private void CreateTable(FunctionNode node)
        {
            if (!node.HasChildNode)
                return;

            int i = 0; TableRow tr = null; int addNodeNum = 0;
            foreach (FunctionNode child in node.Nodes)
            {
                if (this._permission.Check(child.FunctionID))
                {
                    string s_desc = child.Description;
                    if (i == 2)
                    {
                        i = 0;
                    }
                    if (i == 0)
                    {
                        tr = new TableRow();
                        tr.Attributes.Add("onmouseover", "this.style.cursor = 'hand';");
                        //tr.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
                        //tr.Attributes.Add("onclick", "window.navigate('" + _appPath + child.FunctionPath + "');return false;");
                        _table.Rows.Add(tr);
                    }

                    TableCell td = new TableCell();
                    td.Height = Unit.Parse("92");
                    td.HorizontalAlign = HorizontalAlign.Right;
                    td.Width = Unit.Parse("205");
                    tr.Cells.Add(td);
                    Table table = new Table();
                    table.CellPadding = 0;
                    table.CellSpacing = 0;
                    table.Width = Unit.Parse("205");
                    table.Height = Unit.Parse("92");
                    table.Attributes["background"] = string.Format("{0}App_Themes/Default/images/1_pic_3.jpg", this._appPath);
                    td.Controls.Add(table);
                    TableRow ttr = new TableRow();
                    table.Rows.Add(ttr);
                    TableCell ttd = new TableCell();
                    ttr.Cells.Add(ttd);
                    ttd.Height = Unit.Parse("92");
                    ttd.HorizontalAlign = HorizontalAlign.Right;
                    ttd.Width = Unit.Parse("90");
                    ttd.Attributes.Add("onclick", "window.navigate('" + _appPath + child.FunctionPath + "');return false;");

                    ttd = new TableCell();
                    ttr.Cells.Add(ttd);
                    ttd.Height = Unit.Parse("92");
                    ttd.HorizontalAlign = HorizontalAlign.Left;
                    ttd.Width = Unit.Parse("115");
                    ttd.Attributes["style"] = "font-family: 宋体, Arial, Helvetica, sans-serif;  color: #f7941d";//text-decoration: underline;
                    ttd.Attributes.Add("onclick", "window.navigate('" + _appPath + child.FunctionPath + "');return false;");
                    ttd.Attributes.Add("onmouseover", "this.style.textDecorationUnderline=true;");//f7941dthis.style.color = #f7941d;
                    ttd.Attributes.Add("onmouseout", "this.style.textDecorationUnderline=false;");//this.style.color=#FFFFFF;
                    ttd.Text = s_desc;
                    i++;
                    addNodeNum++;
                }
            }
            if (addNodeNum==0)
            {
                tr = new TableRow();
                tr.Attributes.Add("onmouseover", "this.style.cursor = 'hand';");
                //tr.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
                tr.Attributes.Add("onclick", "window.navigate('" + _appPath + "Default.aspx');return false;");
                _table.Rows.Add(tr);
                
                TableCell td = new TableCell();
                tr.Cells.Add(td);
                td.Height = Unit.Parse("92");
                td.HorizontalAlign = HorizontalAlign.Right;
                td.Width = Unit.Parse("90");

                td = new TableCell();
                tr.Cells.Add(td);
                td.Height = Unit.Parse("92");
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Width = Unit.Parse("115");
                td.Attributes["style"] = "font-family: 宋体, Arial, Helvetica, sans-serif;  color: #f7941d";//text-decoration: underline;
                td.Attributes.Add("onmouseover", "this.style.textDecorationUnderline=true;");//f7941dthis.style.color = #f7941d;
                td.Attributes.Add("onmouseout", "this.style.textDecorationUnderline=false;");//this.style.color=#FFFFFF;
                td.Text = "没有权限使用当前页面的功能,点击返回主页!";
            }
        }
    }
}
